package com.py.androidUtils.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Window;

import com.py.androidUtils.view.TitleBarView;
import com.readyidu.app.R;

/**
 * @说明 公共方法
 * @作者 LY
 * @文件 BaseActivity.java
 * @时间 2015年6月11日 下午8:18:14
 * @版权 Copyright(c) 2014 LY-版权所有
 */
public class BaseActivity extends FragmentActivity {
	private TitleBarView titleBarView;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);

	}

	/**
	 * 获取头部标签
	 * 
	 * @return
	 */
	public TitleBarView getTitleActionBar() {
		if (titleBarView == null) {
			titleBarView = new TitleBarView(getWindow());
		}
		return titleBarView;
	}

	/**
	 * 启动activity
	 * 
	 * @param context
	 * @param targetActivity跳转界面
	 * @param extras传递参数
	 */
	public void launcherActivity(Context context, Class<?> targetActivity, Bundle extras) {
		Intent intent = new Intent(context, targetActivity);
		if (null != extras) {
			intent.putExtras(extras);
		}
		context.startActivity(intent);
		((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_out);
	}

	/**
	 * 启动一个activity
	 * 
	 * @param context
	 * @param targetActivity跳转界面
	 * @param extras传递参数
	 * @param requestCode请求码
	 */
	public static void launcherActivityForResult(Context context, Class<?> targetActivity, Bundle extras, int requestCode) {
		Intent intent = new Intent(context, targetActivity);
		if (null != extras) {
			intent.putExtras(extras);
		}
		((Activity) context).startActivityForResult(intent, requestCode);
		((Activity) context).overridePendingTransition(R.anim.push_left_in, R.anim.push_out);
	}

	/**
	 * 关闭一个activity
	 */
	public void finishactivity(Context context) {
		((Activity) context).finish();
		((Activity) context).overridePendingTransition(0, R.anim.push_right_out);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finishactivity(this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
